<template>
  <div class="application-form-container">
    <div class="application-form">
      <div class="filter-container">
        <el-input
          v-model="listQuery.fileName"
          placeholder="文件名称"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
        <el-input
          v-model="listQuery.fileNumber"
          placeholder="文件编号"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          搜索
        </el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
          新增
        </el-button>
      </div>

      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="文件编号" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.fileNumber }}
          </template>
        </el-table-column>
        <el-table-column label="文件名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.fileName }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="180" align="center">
          <template slot-scope="scope">
            {{ formatDateTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              编辑
            </el-button>
            <el-button size="mini" type="success" @click="handleView(row)">
              查看
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.current"
        :limit.sync="listQuery.size"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getDocumentDistributionPage, deleteDocumentDistribution } from '@/api/document-distribution'
import Pagination from '@/components/Pagination'

export default {
  name: 'DocumentDistributionList',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 10,
        fileName: '',
        fileNumber: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getDocumentDistributionPage(this.listQuery).then(response => {
        this.list = response.data.data.records
        this.total = response.data.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push('/main/renshi/jixiao/wjfafanghs')
    },
    handleUpdate(row) {
      this.$router.push({
        path: '/main/renshi/jixiao/wjfafanghs',
        query: { id: row.id, type: 'edit' }
      })
    },
    handleView(row) {
      this.$router.push({
        path: '/main/renshi/jixiao/wjfafanghs',
        query: { id: row.id, type: 'view' }
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除该记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDocumentDistribution(row.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      })
    },
    formatDateTime(date) {
      if (!date) return ''
      const d = new Date(date)
      return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`
    }
  }
}
</script>

<style lang="scss" scoped>
.application-form-container {
  font-family: '宋体', sans-serif;
  padding: 20px;
}

.application-form {
  width: 1200px;
  margin: 0 auto;
}

.filter-container {
  padding-bottom: 10px;
  .filter-item {
    display: inline-block;
    vertical-align: middle;
    margin-bottom: 10px;
    margin-right: 10px;
  }
}

.el-table {
  margin-bottom: 20px;
  font-family: '宋体', sans-serif;
  
  :deep(.el-table__row) {
    height: 50px;
  }
  
  :deep(.el-table__header-wrapper th) {
    background-color: #f5f7fa;
    color: #606266;
    font-weight: bold;
  }
  
  :deep(.cell) {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

@media print {
  .application-form {
    width: 1000px;
    margin: 0px;
  }

  .filter-container {
    display: none;
  }

  .pagination-container {
    display: none;
  }
}
</style> 